package gui;

import java.awt.Color;
import java.awt.Rectangle;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import java.awt.Dimension;
import java.util.Random;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JTextField;

import client.Client;

public class MainCustomer extends JFrame{
	
	final public static int DEFAULT_PORT = 5555;	
    private String host = MainPanel.ip; 
	private JPanel mainform = null;
	private MainPanel MP;
	private JLabel jLabelWelcome = null;
	private JButton jButtonCatalog = null;
	private JButton jButtonOrder = null;
	private JButton jButtonExit = null;
	private JButton jButtonHelp = null;
	private JButton jButtonCancel = null;
	
	private JLabel jLabelLogIn = null;
	JTextField jTextFieldloginInfo = null;	
	private Client client1;
	private JLabel jLabelBackground = null;
	String[] background =new String[]{"1.png","2.jpg","4.jpg","6.jpg"};
	Random generator = new Random();
	private CustomerCatalog cc;
	private Client c = new Client(host,DEFAULT_PORT);
	static String[] DataToTable;	
	public MainCustomer() {
		super();
		initialize();
	}
	

	/**
	 * This method initializes MainCustomer 
	 */
	private void initialize() {
		this.setSize(1000, 720);
		this.setContentPane(MainClientForm());
		this.setTitle("MAIN SCREEN");	
		this.addWindowListener(new java.awt.event.WindowAdapter() {
			public void windowClosing(java.awt.event.WindowEvent e) {
				Client closeprog = new Client(host,DEFAULT_PORT);
				closeprog.accept(gui.MainPanel.Userinfo);										
				System.exit(0);	
			}
		});
		this.cc = new CustomerCatalog();
		this.cc.backMainButton.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent e) {
				setContentPane(mainform);
			}
		});
		
	}
	/**
	 * this method initializes MainClientForm panel
	 * @return mainform
	 */
	private JPanel MainClientForm() {
		if (mainform == null) {
			int randomIndex = generator.nextInt( 4 );
			ImageIcon imageBack = new ImageIcon(background[randomIndex]);			
			jLabelBackground = new JLabel();
			jLabelBackground.setIcon(imageBack);
			jLabelBackground.setBounds(new Rectangle(1, -2, 990, 689));						 				
			ImageIcon welcome = new ImageIcon("welcome1");				
			jLabelLogIn = new JLabel();
			jLabelLogIn.setBounds(new Rectangle(285, 616, 168, 35));
			jLabelLogIn.setText("YOUR LAST LOGIN WAS ON:");			
			jLabelWelcome = new JLabel();
			jLabelWelcome.setIcon(welcome);
			jLabelWelcome.setBounds(new Rectangle(213, 29, 556, 138));
			//jLabelWelcome.setText("WELCOME TO ZER LEHA");
			mainform = new JPanel();
			mainform.setLayout(null);
			mainform.setBackground(Color.CYAN); 
			mainform.add(getJButtonCatalog(), null);
			mainform.add(getJButtonOrder(), null);
			mainform.add(getJButtonExit(), null);
			mainform.add(getJButtonHelp(), null);
			
			mainform.add(getJButtonCancel(), null);
			mainform.add(jLabelLogIn, null);
			mainform.add(getJTextFieldloginInfo(), null);
			mainform.add(jLabelWelcome, null);
			mainform.add(jLabelBackground, null);
		}
		return mainform; 
	}

	/**
	 * This method initializes jButtonCatalog and opens the catalog screen	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButtonCatalog() {
		if (jButtonCatalog == null) {
			jButtonCatalog = new JButton();
			ImageIcon imageCatalog = new ImageIcon("catalog.gif");
			jButtonCatalog.setIcon(imageCatalog);
			jButtonCatalog.setBounds(new Rectangle(332, 202, 282, 105));
			jButtonCatalog.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					cc.jTabbedPane1.setVisible(true);
					cc.cartPanel.setVisible(false);
					for (int i=0;i<cc.cart.size();i++){
						cc.cart.get(i).setQuntity(0);
					}
					setContentPane(cc);
				}
			});
		}
		return jButtonCatalog;
	}

	/**
	 * This method initializes jButtonOrder	and opens the order screen
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButtonOrder() {
		if (jButtonOrder == null) {
			jButtonOrder = new JButton();
			ImageIcon imageOrder = new ImageIcon("order1.PNG");
			jButtonOrder.setIcon(imageOrder);
			jButtonOrder.setBounds(new Rectangle(330, 324, 284, 132));
		//	jButtonOrder.setText("MAKE AN ORDER");
			jButtonOrder.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					//order need to get client as object 
					
					Order1 Order1Frame = new Order1(1);
					
					Order1Frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
					Order1Frame.setSize( 600,800 ); // set frame size
					Order1Frame.setResizable(false);
					Order1Frame.setVisible( true ); // display frame
					
				    
				    
				}
			});
		}
		return jButtonOrder;
	}

	/**
	 * This method initializes jButtonExit and opens the exit screen
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButtonExit() {
		if (jButtonExit == null) {
			jButtonExit = new JButton();
			ImageIcon imageCancel = new ImageIcon("Button_cancel.png");
			jButtonExit.setIcon(imageCancel);
			jButtonExit.setBounds(new Rectangle(816, 564, 101, 93));
		//	jButtonExit.setText("EXIT");
			jButtonExit.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					
					ExitFrame EFrame = new ExitFrame();
				    EFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
				    EFrame.setSize( 300,200 ); // set frame size
				    EFrame.setResizable(true);
				    EFrame.setVisible( true ); // display frame
		
				    
				    
				}
			});
		}
		return jButtonExit;
	}

	/**
	 * This method initializes jButtonHelp and opens the help screen
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButtonHelp() {
		if (jButtonHelp == null) {
			jButtonHelp = new JButton();
			ImageIcon imageHelp = new ImageIcon("help.jpg");
			jButtonHelp.setIcon(imageHelp);
			jButtonHelp.setBounds(new Rectangle(62, 572, 90, 82));
	//		jButtonHelp.setText("HELP");
			jButtonHelp.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					HelpFrame HFrame = new HelpFrame();

					HFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
					HFrame.setSize( 600,720 ); // set frame size
					HFrame.setResizable(false);
					HFrame.setVisible( true ); // display frame
				}
			});
		}
		return jButtonHelp;
	}

	/**
	 * This method initializes jButtonCancel and opens the cancel order screen	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButtonCancel() {
		if (jButtonCancel == null) {
			jButtonCancel = new JButton();
			ImageIcon imageCancel = new ImageIcon("order-cancellation.jpg");
			jButtonCancel.setIcon(imageCancel);
			jButtonCancel.setBounds(new Rectangle(330, 474, 284, 100));
			jButtonCancel.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					//order need to get client as object 
					
					
					
					OrderCancel CFrame = new OrderCancel();

					CFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
					CFrame.setSize( 1000,720 ); // set frame size
					CFrame.setResizable(false);
					CFrame.setVisible( true ); // display frame
					
					Client SearchOrders = new Client(host,DEFAULT_PORT);					
					Vector resalts = new Vector<Object>();
					String[] GetOrders= new String[2];
					GetOrders[0]="get_orders";
					GetOrders[1]=MainPanel.Userinfo[3];
					SearchOrders.accept(GetOrders);
					Object p = SearchOrders.getMsg();
					resalts = (Vector)p;					
					for (int i=0;i<=resalts.size();i++)
					{
						Vector temp=(Vector) resalts.get(i);
						DataToTable = new String[4];
						DataToTable[0]=Integer.toString((Integer)temp.get(0));
						DataToTable[1]=Integer.toString((Integer)temp.get(1));
						DataToTable[2]=Integer.toString((Integer)temp.get(2));						
						DataToTable[3]=(String) temp.get(3);											
						OrderCancel.model.insertRow(i, DataToTable);
					}
				}
			});
		}
		return jButtonCancel;
	}

	/**
	 * This method initializes jTextFieldloginInfo	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJTextFieldloginInfo() {
		if (jTextFieldloginInfo == null) {
			jTextFieldloginInfo = new JTextField();
			jTextFieldloginInfo.setBounds(new Rectangle(465, 616, 137, 35));
		}
		return jTextFieldloginInfo;
	}


	/**
	 * This method initializes jButtoncomplaint	
	 * 	
	 * @return javax.swing.JButton	
	 */
	
}  //  @jve:decl-index=0:visual-constraint="26,28"
